package com.zjitc.service.impl;

import com.zjitc.common.ServerResponse;
import com.zjitc.dao.BackstageDao;
import com.zjitc.dao.ProductDao;
import com.zjitc.dao.UserDao;
import com.zjitc.model.Backstage;
import com.zjitc.model.Pay;
import com.zjitc.model.Product;
import com.zjitc.model.User;
import com.zjitc.service.BackstageService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/5/31 15:00
 * @description:
 */
@Service
public class BackstageServiceImpl implements BackstageService {

  @Autowired
  private BackstageDao backstageDao;

  @Autowired
  private ProductDao productDao;

  @Autowired
  private UserDao userDao;

  @Override
  public ServerResponse<User> login(@RequestParam("backstagename") String backstagename,
      @RequestParam("backstagepassword") String backstagepassword) {
    int count = backstageDao.checkUser(backstagename);
    if (count != 1) {
      return ServerResponse.createError("用户不存在");
    }
    User backstage = backstageDao
        .findBybackstageNameAndbackstagePassword(backstagename, backstagepassword);
    if (backstage == null) {
      return ServerResponse.createError("密码错误");
    }
    backstage.setQuestion(null);
    backstage.setAnswer(null);
    return ServerResponse.createSucess(backstage);
  }

  @Override
  public List<Product> backstageshowProduct() {
    List<Product> backstage = backstageDao.backstageshowProduct();
    return backstage;
  }

  @Override
  public void backstagedelete(Integer id) {
    productDao.delete(id);
  }

  @Override
  public Product updatebackstage(Integer id) {
    return null;
  }

  @Override
  public Product findproductById(Integer id) {
    Product product=productDao.findproductById(id);
      return product;
  }

  @Override
  public void updateback(Product product) {
    productDao.updateProduct(product);
  }

  @Override
  public List<User> findAllUser() {
    List<User> userList = backstageDao.findAllUser();
    return userList;
  }

  @Override
  public User findUserById(Integer id) {
    User user=userDao.findUserById(id);
    return user;
  }

  @Override
  public void userEdit(User user) {
    userDao.updateUser(user);
  }

  @Override
  public void userdelete(Integer id) {
    backstageDao.userdelete(id);
  }

  @Override
  public List<Pay> findMyPay() {
    return backstageDao.findMyPay();
  }

  @Override
  public void paydelete(Integer id) {
    backstageDao.paydelete(id);
  }


}
